package com.ruoyi.pay.config;

import com.alipay.api.AlipayApiException;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import com.ruoyi.pay.entity.AliPayBean;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@AllArgsConstructor
public class AliPayConfig {
    private final AliPayBean properties;

    @Bean
    @ConditionalOnMissingBean
    public AliPayApiConfig aliConfig() throws AlipayApiException {

        AliPayApiConfig apiConfig = AliPayApiConfig.builder()
                .setAppId(properties.getAppId())
                .setAliPayPublicKey(properties.getPublicKey())
                .setAppCertPath(properties.getAppCertPath())
                .setAliPayCertPath(properties.getAliPayCertPath())
                .setAliPayRootCertPath(properties.getAliPayRootCertPath())
                .setCharset("UTF-8")
                .setPrivateKey(properties.getPrivateKey())
                .setServiceUrl(properties.getServerUrl())
                .setSignType("RSA2")
                // 普通公钥方式
                //.build();
                // 证书模式
                .buildByCert();

        AliPayApiConfigKit.putApiConfig(apiConfig);

        return AliPayApiConfigKit.getAliPayApiConfig();
    }
}
